#!/bin/bash

TOPDIR=$(cd $(dirname "$0") && pwd)
TEMP=`mktemp`; rm -rfv $TEMP >/dev/null; mkdir -p $TEMP;

rpm_dash=`rpm -qa | grep vsm-dashboard`
rpm_dash=${rpm_dash##*board-}
rpm_dash=${rpm_dash%%.el6*}

vsm-manage db sync
VSM_VERSION=$rpm_dash
sed -i "s,%VSM_VERSION%,$VSM_VERSION,g" /usr/share/vsm-dashboard/vsm_dashboard/dashboards/vsm/overview/summarys.py

# Below is used to setup https

function set_password {
    var=$1; msg=$2
    pw=${!var}
    localrc=/etc/vsmdeploy/deployrc

    if [ ! $pw ]; then
        if [ ! -e $localrc ]; then
            touch $localrc
        fi
        pw=" "
        if [ ! $pw ]; then
            pw=`openssl rand -hex 10`
        fi
        eval "$var=$pw"
    fi
    sed -i "s,$var=.*,$var=$pw,g" $localrc
}

set_password VSM_HTTPS_PASSWORD
VSM_HTTPS_PASSWORD=${VSM_HTTPS_PASSWORD:-https_password}
source $TOPDIR/keys/https
setup_https

restart-all
